//
//  RefundViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/7.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "RefundViewController.h"
#import "RefundCell.h"
#import "MineAllOrderViewController.h"
@interface RefundViewController ()<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *datasource;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *aview;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) UIButton *refundBtn;
@property (nonatomic, copy) NSString *str;//判断用于点击那个cell的

@end

@implementation RefundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackItem];
    self.title = @"退款";
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, 55)];
    bgView.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:bgView];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, GWIDTH - 100, 50)];
    titleLabel.text = @"  爱儿美儿童舞蹈秋季梦想儿童舞蹈";
    titleLabel.text = [NSString stringWithFormat:@"   %@", self.goodName];
    titleLabel.backgroundColor = ColorWhite;
    titleLabel.textColor = ColorFrom16(0x7F7E7D);
    titleLabel.font = PFRFONT(14);
    [bgView addSubview:titleLabel];
    
    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(GWIDTH - 100, 0, 100, 50)];
    priceLabel.text = @"580元";
    priceLabel.text = self.sale_price;
    priceLabel.backgroundColor = ColorWhite;
    priceLabel.textAlignment = 1;
    priceLabel.textColor = ColorFrom16(0x7F7E7D);
    priceLabel.font = PFRFONT(12);
    [bgView addSubview:priceLabel];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 55, GWIDTH, 230) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableFooterView = [UIView new];
    [_tableView registerClass:[RefundCell class] forCellReuseIdentifier:@"cell"];

    [self.view addSubview:_tableView];
    
    _refundBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_refundBtn setTitle:@"确认退款" forState:0];
    [_refundBtn setTitleColor:ColorFrom16(0x202020) forState:0];
    _refundBtn.titleLabel.font = PFRFONT(16);
    _refundBtn.layer.backgroundColor = ColorFrom16(0xf0f0f0).CGColor;
    _refundBtn.layer.cornerRadius = 7;
    [_refundBtn addTarget:self action:@selector(handleRefundBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_refundBtn];
    [_refundBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableView.mas_bottom).offset(44);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@44);
    }];
    [self createReminderView];
    _datasource = [NSMutableArray arrayWithObjects:@"不想要了", @"太贵", @"我想换其它的", @"服务不好", nil];
    
    _indexPath = nil;
    
}

- (void)startNetwork
{
    NSString *reason = self.datasource[_indexPath.row];
//    if([GW_Tool isEmptyString:reason])
//    {
//        MESSAGE_TOAST(@"请选择退款原因");
//        return;
//    }
    
    NSDictionary *param = @{@"order_sn":self.orderId,
                            @"refund_reason":reason};
    [NetWork sendPostRequestURL:[NSString stringWithFormat:@"%@/order/OrderAllRefund", GW_Domain] parameters:param completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"退款 %@", resultDict);
        [UIView animateWithDuration:1.0 animations:^{
            _bgView.hidden = NO;
        }];
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", error);
    }];
}


//确认退款响应按钮
- (void)handleRefundBtn:(UIButton *)sender
{
    if([sender.backgroundColor isEqual:ColorFrom16(0xFEDE6D)])
    {
        [self startNetwork];
    }
    else
    {
        MESSAGE_TOAST(@"请选择退款原因");
    }
    

   
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = ColorWhite;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, 49)];
    label.text = @"退款原因";
    label.textColor = ColorFrom16(0x333333);
    label.textAlignment = 1;
    label.font = PFMFONT(14);
    [view addSubview:label];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 49, GWIDTH, 1)];
    line.backgroundColor = COLOR_BACKGROUND;
    [view addSubview:line];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RefundCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.label.text = self.datasource[indexPath.row];
    if([cell.label.text isEqualToString:_str])
    {
        cell.selectImg.hidden = NO;
    }
    else
    {
        cell.selectImg.hidden = YES;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    _indexPath = indexPath;
    _str = self.datasource[indexPath.row];
    [_tableView reloadData];
    _refundBtn.backgroundColor = ColorFrom16(0xFEDE6D);
}


- (void)handleTapAction
{
    [UIView animateWithDuration:1.0 animations:^{
        _bgView.hidden = YES;
    }];
}


- (void)createReminderView
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT)];
    _bgView.backgroundColor = ColorFrom16A(0x000000, 0.8);
    _bgView.hidden = YES;
    [self.navigationController.view addSubview:_bgView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction)];
    tap.delegate = self;
    [_bgView addGestureRecognizer:tap];
    
    _aview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 180)];
    _aview.layer.backgroundColor = ColorWhite.CGColor;
    _aview.layer.cornerRadius = 12;
    _aview.center = _bgView.center;
    [_bgView addSubview:_aview];
    
    UIImageView *iv = [UIImageView new];
    iv.image = IMAGE(@"correct");
    iv.layer.backgroundColor = ColorClear.CGColor;
    iv.layer.cornerRadius = 67 / 2;
    [_aview addSubview:iv];
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_aview).offset(-29);
        make.centerX.equalTo(_aview.mas_centerX);
        make.width.equalTo(@67);
        make.height.equalTo(@67);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"您已申请退款";
    titleLabel.textColor = ColorFrom16(0xF5CB37);
    titleLabel.font = PFRFONT(17);
    [_aview addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iv.mas_bottom).offset(24);
        make.centerX.equalTo(_aview.mas_centerX);
        make.width.greaterThanOrEqualTo(@100);
        make.height.equalTo(@24);
    }];
    
    UILabel *label = [UILabel new];
    label.text = @"审核会在24小时之内完成。审核进度，您可到“我的-退款”中进行查看。";
    label.textColor = ColorFrom16(0xA7A7A7);
    label.numberOfLines = 2;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = 1;
    label.font = PFRFONT(13);
    [_aview addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.width.equalTo(@240);
        make.height.equalTo(@32);
        make.centerX.equalTo(_aview.mas_centerX);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"立即查看" forState:0];
    [button setTitleColor:ColorFrom16(0x323232) forState:0];
    button.titleLabel.font = PFRFONT(17);
    button.backgroundColor = ColorFrom16(0xFEDE6D);
    [_aview addSubview:button];
    button.frame = CGRectMake(0, 140, 270, 40);
    [button addTarget:self action:@selector(handlebtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //切圆角  任意切两个
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = button.bounds;
    maskLayer.path = maskPath.CGPath;
    button.layer.mask = maskLayer;
}
//立即查看
- (void)handlebtn:(UIButton *)sender
{
    MineAllOrderViewController *all = [MineAllOrderViewController new];
    all.titleName = @"退款";
    all.status = @"6";//待付款
    all.flag = @"订单";
    [self.navigationController pushViewController:all animated:YES];
    _bgView.hidden = YES;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([touch.view isDescendantOfView:_aview])
        return NO;
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
